package org.example.controller.iim;

import jakarta.annotation.Resource;
import org.example.domain.Result;
import org.example.domain.iim.Projects;
import org.example.domain.iim.Relations;
import org.example.mapper.ProjectsMapper;
import org.example.mapper.RelationsMapper;
import org.example.utils.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:
 * @Author: whopxx
 * @CreateTime: 2025-01-06
 */
@RestController
@RequestMapping("/projects")
public class ProjectsController {

    @Resource
    private ProjectsMapper projectsMapper;

    @Resource
    private RelationsMapper relationsMapper;

    @GetMapping("/list")
    public Result list() {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer nowId = (Integer) claims.get("id");
        return Result.success(projectsMapper.selectListByUserId(nowId));
    }

    @PostMapping
    public Result add(@RequestBody Projects projects) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer nowId = (Integer) claims.get("id");
        projectsMapper.insert(projects);
        // 插入关联数据
        Relations relations = new Relations();
        relations.setProjectId(projects.getId());
        relations.setUserId(nowId);
        relationsMapper.insert(relations);
        return Result.success();
    }


    @PutMapping
    public Result update(@RequestBody Projects projects) {
        projectsMapper.updateById(projects);
        return Result.success();
    }

    @DeleteMapping
    public Result delete(@RequestParam Integer id) {
        projectsMapper.deleteById(id);
        return Result.success();
    }
}
